
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local RankDefine = require("rank.RankDefine")
local Msg = require("core.Msg")
local SbConfig = require("excel.Shenbing").shenbing
local Log = require("common.Log")
local PfLogic = require("tencentApi.PfLogic")

SHENBING_RANK = SHENBING_RANK or {}
--SHENBING_RANK = {index = {uuid,name,sbName, sbPower, create = os.time()},index = {uuid,name,sbName, sbPower}}

function setRankList(human, list, index)
  list.type = RankDefine.SB_RANK_TYPE
  list.name = SHENBING_RANK[index].name
  list.uuid = SHENBING_RANK[index].uuid
  list.sbName = SHENBING_RANK[index].sbName
  list.sbPower = SHENBING_RANK[index].sbPower
  if PfLogic.isBlueShow(human) then
    list.blue = SHENBING_RANK[index].blue
  else
    list.blue = "0000"
  end
  list.args = 0
end

function GetRankList(human, sendMsg)
  sendMsg.sblist[0] = #SHENBING_RANK
  for i=1, #SHENBING_RANK do
    if SHENBING_RANK[i].uuid == human._id then
      sendMsg.myRank = i
    end
    setRankList(human, sendMsg.sblist[i], i)
  end 
  Msg.send(sendMsg, human.fd)
end

function getFirst(human, list)  
  list[0] = 0 
  if SHENBING_RANK[1] then
    list[0] = list[0] + 1
    setRankList(human, list[list[0]], 1)
  end
end

function getSbMingzi(human)
  for i=0, human.db.shenbingLen - 1 do
    if human.db.shenbing[i].id == human.db.putonShenbing then
      local mingzi = ""
      mingzi = SbConfig[human.db.putonShenbing].sbMingzi
      if human.db.shenbing[i].jinhua == 1 then
        mingzi = "真·"..mingzi
      end
      return mingzi
    end
  end
  return nil
end

function setRankInfo(human, index, sbPower)
  local mingzi = getSbMingzi(human)
  SHENBING_RANK[index] = {}
  SHENBING_RANK[index].uuid = human._id  
  SHENBING_RANK[index].sbName = mingzi
  SHENBING_RANK[index].name = human.name
  SHENBING_RANK[index].sbPower = sbPower
  SHENBING_RANK[index].account = human.account
  SHENBING_RANK[index].create = os.time()
  SHENBING_RANK[index].blue = string.format("%d%d%d%d",human.db.blueVipLv, human.db.blueYearVip, human.db.blueSuperVip, human.db.level3366)
end

function sortFunc(a, b)
  if a.sbPower == b.sbPower then
    return a.create < b.create
  end
  return a.sbPower > b.sbPower
end

function isInRank(human, sbPower)
  for i=1, #SHENBING_RANK do
    if SHENBING_RANK[i].uuid == human._id then
      if sbPower ~= SHENBING_RANK[i].sbPower then
        setRankInfo(human, i, sbPower)
        table.sort(SHENBING_RANK, sortFunc)
      end
      return true
    end
  end
  return false
end
--得到神兵战力
function getSbPower(human)
  local power = 0
  for i=0, human.db.shenbingLen - 1 do
    power = power + human.db.shenbing[i].zhandouli
  end
  return power
end

--外部调用，神兵变更的时候调用
function sbPowerChange(human)
  if human.db.lv < 35 then return end
  local sbPower = getSbPower(human)
  if isInRank(human, sbPower) then
    return
  end
  if #SHENBING_RANK < RankDefine.RANK_MAX_CNT then
    setRankInfo(human, #SHENBING_RANK + 1, sbPower)
  elseif #SHENBING_RANK >= RankDefine.RANK_MAX_CNT and sbPower > SHENBING_RANK[#SHENBING_RANK].sbPower then
    setRankInfo(human, RankDefine.RANK_MAX_CNT, sbPower)
  else
    return false
  end
  table.sort(SHENBING_RANK, sortFunc)
end

function getRank()
  local dbType = {type = RankDefine.SB_RANK_TYPE}
  LuaMongo.find(DB.db_rank, dbType)
  while true do
    local tmp = {}
    if not LuaMongo.next(tmp) then
      break
    end
    SHENBING_RANK[tmp.index] = {}
    SHENBING_RANK[tmp.index].uuid = tmp.uuid
    SHENBING_RANK[tmp.index].name = tmp.name
    SHENBING_RANK[tmp.index].sbName = tmp.sbName
    SHENBING_RANK[tmp.index].sbPower = tmp.sbPower
    SHENBING_RANK[tmp.index].account = tmp.account
    SHENBING_RANK[tmp.index].create = tmp.create or os.time()
    SHENBING_RANK[tmp.index].blue = tmp.blue or "0000"
  end
end

function wrapperSBData(index)
  local db = {}
  db.type = RankDefine.SB_RANK_TYPE
  db.index = index
  db.uuid = SHENBING_RANK[index].uuid
  db.name = SHENBING_RANK[index].name
  db.sbName = SHENBING_RANK[index].sbName
  db.sbPower = SHENBING_RANK[index].sbPower
  db.account = SHENBING_RANK[index].account
  db.create = SHENBING_RANK[index].create or os.time()
  db.blue = SHENBING_RANK[index].blue or "0000"
  return db
end

function saveRank()
  for i=1, #SHENBING_RANK do
    local db = wrapperSBData(i)
    LuaMongo.insert(DB.db_rank, db)
  end
end

function writeLog()
  for i=1, #SHENBING_RANK do
    Log.write(Log.LOGID_OSS_SB_RANK, SHENBING_RANK[i].uuid, SHENBING_RANK[i].account, SHENBING_RANK[i].name, SHENBING_RANK[i].sbPower, i)
  end
end